﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MyWeb.Business;
using MyWeb.Common;
using MyWeb.Data;

namespace MyWeb.Module.Product
{
    public partial class DetailPro : System.Web.UI.Page
    {
        string Id = "";
        string GrId = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.RouteData.Values["Id"] != null)
            {
                Id = Page.RouteData.Values["Id"].ToString();
            }
            if (Page.RouteData.Values["GrId"] != null)
            {
                GrId = Page.RouteData.Values["GrId"].ToString();
            }
            if (!IsPostBack)
            {
                List<Data.Product> list = ProductService.Product_GetById(Id);
                {
                    if (list.Count > 0)
                    {
                        LoadMetaConfig(list[0].Name, list[0].Name, list[0].Name + " tranh my nghe", StringClass.NameToTag(list[0].Name));
                        lsvdetail.DataSource = ProductService.Product_GetById(Id);
                        lsvdetail.DataBind();
                        ShowProduct();
                    }
                }

                SqlDataProvider sql = new SqlDataProvider();
                sql.ExecuteNonQuery("update [Product] set Views=Views+1 where Id=" + Id + "");

            }
        }
        protected void ShowProduct()
        {

            List<Data.Product> list1 = ProductService.Product_GetByTop("6", "GroupId=" + GrId + "", "Date DESC");
            ltrPro.Text += "<div class=\"bodyboxpro\"> \n";
            for (int j = 0; j < list1.Count; j++)
            {
                ltrPro.Text += "  <div class=\"prohome grid\"> \n";
                ltrPro.Text += "<div class=\"div_img\"> \n";
                ltrPro.Text += " <a class=\"tooltip\" href=\"/Pro/" + list1[j].GroupId + "/" + list1[j].Id + "/" + StringClass.NameToTag(list1[j].Name) + ".aspx\"><img src=\"" + list1[j].Image + "\" alt=\"" + list1[j].Name + "\"></a>";
                ltrPro.Text += "</div>  \n";
                ltrPro.Text += "<a class=\"productname tooltip\" href=\"/Product/" + list1[j].Id + "/" + StringClass.NameToTag(list1[j].Name) + ".aspx\">" + list1[j].Name + "</a> \n";
                ltrPro.Text += "<div class=\"price\"><div class=\"gia\"><span>Giá bán:</span>" + Formatmoney(list1[j].PriceNew) + "</div></div> \n";
                ltrPro.Text += "<div class=\"dathang\">";
                ltrPro.Text += "<div class=\"order\"><a href=\"/Shop/" + list1[j].Id + ".aspx\" title=\"\">Đặt mua</a></div> \n";
                ltrPro.Text += "<div class=\"detail\"><a href=\"/Pro/" + list1[j].GroupId + "/" + list1[j].Id + "/" + StringClass.NameToTag(list1[j].Name) + ".aspx\" title=\"\">Chi tiết</a></div> \n";
                ltrPro.Text += "</div>  \n";

                ltrPro.Text += "<pre class=\"hidden\"><div class=\"boxpro\">  \n";
                ltrPro.Text += "<div class=\"proname\">" + list1[j].Name + "</div> \n";
                ltrPro.Text += "<div class=\"proimg1\"><a href=\"\"><img src=\"" + list1[j].Image + "\" align=\"center\"></a></div> \n";
                ltrPro.Text += "<div class=\"clear\"></div><div class=\"contentpro\"></div><div class=\"teaser\"></div> \n";
                ltrPro.Text += "</div></pre> \n";
                ltrPro.Text += "</div> \n";
            }
            ltrPro.Text += "<div class=\"clear\"></div> \n";
            ltrPro.Text += "</div>";
        }
        protected string Url(string GrId, string Id, string Name)
        {
            return "/Pro/" + GrId + "/" + Id + "/" + StringClass.NameToTag(Name) + ".aspx";
        }
        protected string Formatmoney(string money)
        {
            string tien = "";
            try
            {
                tien = "" + Double.Parse(money).ToString("#,000") + " VNĐ";
            }
            catch
            {
                return tien = "";
            }
            return tien;
        }
        void LoadMetaConfig(string strName, string strTitle, string strDescription, string strKeyword)
        {
            Page.Title = strTitle != "" ? strTitle : strName;
            Page.MetaDescription = strDescription != "" ? strDescription : GlobalClass.Description;
            Page.MetaKeywords = strKeyword != "" ? strKeyword : GlobalClass.Keyword;            

        }
        protected void lbtmuahang(object sender, EventArgs e)
        {
            
            foreach (ListViewItem item in this.lsvdetail.Items)
            {                
                Session["Id"] = Id;
                Session["quantity"] = 1;
            }
            Response.Redirect("/Module/Shop/GioHang.aspx");
        }
    }
}